
	var member ;
	var order = {};
	var addressList ;
	var selectAddress ;	
	var orderItem = [];
	
	var buyNum;
	var freight=0;  //运费
	var activityId;
	
	var source;
	var product;
	
	apiready = function() {
        
        activityId = api.pageParam.activityId;
        source=api.pageParam.source;  //来源

        if(source=='scan'){  //扫码
           $api.css($api.byId('shouhuoren'),'display:none');
           
        }
	    member = $api.getStorage('member');  //会员
	    	
		product = api.pageParam.product;
	    buyNum=product.num;
	    
	    calculation();  //计算费用
	    
		//显示购买的商品
		showBuyData();
		
	
		
		
		//监听选择收货地址
		api.addEventListener({
		    name: 'selectaddress'
		}, function(ret){
		    if(ret && ret.value){
		        showaddress(ret.value);
		    }
		});
		
		
	}
	
	
	
	//打开选择地址
	function openaddress(){
	     api.openWin({
	        name: 'address',
	        url: 'address.html',
	        bounces: false,
	        vScrollBarEnabled:false,
	        hScrollBarEnabled:false,
	       }
	    );
	};
	
	
	//显示收货信息
	function showaddress(address){
	    
	    
	    var currAddressDetail=address.province+address.city+address.district+address.address;
	    $api.text($api.byId('receiver'), address.receiver);
	    $api.text($api.byId('mobile'), address.mobile);
	    $api.text($api.byId('address'), currAddressDetail);
	    
	    
	    
	    $api.css($api.byId('noaddress'),'display:none');
	    $api.css($api.byId('yesaddress'),'display:block');
	    
	    order.receiver=address.receiver;
	    order.mobile=address.mobile;
		order.province=address.province;
		order.city=address.city;
		order.district=address.district;
		order.currAddressDetail=currAddressDetail;
	    
	}
	

	
	//购买的商品
	var showBuyData = function() {
		api.showProgress({
	        title: '加载中...',
	        modal: false
	    });
		
      	
		showProduct();
		api.hideProgress();
	}
	
	
	var showAddress = function(){
    if(addressList)$api.byId('addressList').innerHTML = '';
    
	    	for(var i=0;i<addressList.length;i++){
	    		var aHtml = '';
	    		aHtml+='<li id="li'+i+'" class="" name="addressLi" onclick="doselectedAddress('+i+');">';
						aHtml+='<input type="radio" name="location"  value="'+i+'">';
						aHtml+='<p class="ng-binding">';
							aHtml+=addressList[i].receiver;
						aHtml+='</p>';
						aHtml+='<p class="am-text-truncate ng-binding">';
							aHtml+=addressList[i].province+addressList[i].city+addressList[i].district+'	&nbsp;'+addressList[i].address;
						aHtml+='</p>';
						aHtml+='<p class="ng-binding">';
							aHtml+=addressList[i].mobile;
						aHtml+='</p>';
						aHtml+='<span><i class="am-icon-check"></i></span>';
					aHtml+='</li>';
				
				$api.append($api.byId('addressList'), aHtml);
	    	}
	}
	
	
	
	var showProduct = function(){
			var aHtml = '';
			
			 aHtml+='<div class="am-u-sm-12" style="padding: 0px;">';
		            aHtml+='<div class="am-u-sm-3" style="padding:8px 0px;">';
		            aHtml+='<img src="'+serviceUrl+"/files/"+(product.proDefImg||0)+'" style="width:80px;height: 80px"/>';
		            aHtml+='</div>';
		            aHtml+='<div class="am-u-sm-9" style="padding:5px 0px">';
			            aHtml+='<div class="am-text-xs am-text-truncate" style="color:black">'+product.name+'</div>';
			            aHtml+='<div style="color:red;font-size:15px;">￥'+ product.price.toFixed(2)+'</div>';
			            aHtml+='<div>';
				            aHtml+='<div class="aui-counter">';
					            aHtml+='<div class="aui-counter-minus" onclick="reduce();"></div>';
					            aHtml+='<input class="aui-counter-input" readonly id="buyNum" value="'+ (product.num||1) +'">';
					            aHtml+='<div class="aui-counter-plus" onclick="add();"></div>';
				            aHtml+='</div>';
		               aHtml+='</div>';
	               
	               aHtml+='</div>';
	            aHtml+='</div>';
			
			
	
		  $api.html($api.byId('goodList'), aHtml);
 
			
	}
	
	var addOrder = function() {
	        
	        
		    if(order.receiver==undefined){
		         if(source=='scan'){
		         
		         }else{
		             toast('请选择收货信息');
				     return;
		         }
		    	
		    }
		
			orderItem={
			    'id' : product._id,
		    	'num' : buyNum,
		    	'price' : product.price,
		    	'total' :(product.price*buyNum).toFixed(2),
		    	'proDefImg' : product.proDefImg,
		    	'proName' : product.name,
		    	'merName' :product.merchantName,
		    	'purprice' : (product.purprice).toFixed(2),
		    	'code' : product.code,
			};
				

		    if(source=="抢购"){
		       order.buytype = "抢购";
		       order.activityId = activityId;
		    }else if(source=="scan"){
		       order.buytype = "扫码";
		    }else{
		       order.buytype='普通购买';
		    }
				
			order.item = orderItem;
			order.payType=$api.val($api.dom('input[name="payType"]:checked'));
			order.memberAccount = member.account;
			order.remarks=$api.val($api.byId('remarks'));
			order.recman=$api.val($api.byId('recman'));
			order.source=api.deviceName;
			order.buyNum=buyNum;
		   //order.freight=0;
		   //alert(order.freight)
		  //return;
		
            do_ajax('/app/member/addorder2','post',order,function(respose,err){
				if (respose.resultCode == "0") {
		                api.hideProgress();
						var orderinfo= {
							orderNo: respose.orderNo,
							total: order.total,
							payType: order.payType,
						};
						
					   api.openWin({
					        name: 'pay',
					        url: "../pay.html",
					        pageParam: {orderinfo:orderinfo},
					       }
					   );

				} else
					alert(respose.datas);
			});
			
			
			
		
	}
	

	/*
	 * 加法
	 */
	function add(){
	    	if(buyNum != -1) {
			++buyNum;
			$api.val($api.byId('buyNum'),buyNum);
		}
		calculation();
	};

	/*
	 * 减法
	 */
	function reduce(){
	    	if(buyNum>1) {
			--buyNum;
			$api.val($api.byId('buyNum'),buyNum);
		}
		calculation();
	};


	
	/**
	 * 计算费用
	 */
	var calculation = function() {
		
		var  proTotal=product.price*buyNum; //商品金额
		
		if(source=="scan" || product.isfreefreight == '是' || source == '抢购'){
		    freight=0;
		}else{
		
			if(product.price==0){
		       freight=0;
		    }
			if(product.type!='服务产品'){
		          if(proTotal<88){
		             freight=12;
		          }
		          
		    }else{
		        freight=0;
		    }
		    
		    if(proTotal>88){
		        freight=0;
		    }
		    
		    if(proTotal>500){
		       if(product.type!='服务产品'){
		             freight=-20;
		       }
		    }
	    }
	    
	    order.freight=freight;
	    order.proTotal=proTotal.toFixed(2);
	    order.total=(proTotal+freight).toFixed(2);
	    order.rTotal=(proTotal+freight).toFixed(2);
	    
	    
	    
	    $api.text($api.byId('proTotal'), order.proTotal);
	    $api.text($api.byId('freight'),  freight);
	    $api.text($api.byId('total'), order.total);
	    

	}
	
	
	
	